home *** CD-ROM | disk | FTP | other *** search
- package javax.swing.text.rtf;
-
- import java.awt.Color;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.StreamTokenizer;
- import java.net.URL;
- import java.util.Dictionary;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import javax.swing.text.MutableAttributeSet;
- import javax.swing.text.SimpleAttributeSet;
- import javax.swing.text.Style;
- import javax.swing.text.StyledDocument;
-
- class RTFReader extends RTFParser {
- StyledDocument target;
- Dictionary parserState;
- Destination rtfDestination;
- MutableAttributeSet documentAttributes;
- Dictionary fontTable;
- Color[] colorTable;
- Style[] characterStyles;
- Style[] paragraphStyles;
- Style[] sectionStyles;
- int rtfversion;
- boolean ignoreGroupIfUnknownKeyword;
- int skippingCharacters;
- private static Dictionary straightforwardAttributes = RTFAttributes.attributesByKeyword();
- private MockAttributeSet mockery;
- static Dictionary textKeywords = null;
- static final String TabAlignmentKey = "tab_alignment";
- static final String TabLeaderKey = "tab_leader";
- static Dictionary characterSets;
- static boolean useNeXTForAnsi;
-
- static {
- textKeywords = new Hashtable();
- textKeywords.put("\\", "\\");
- textKeywords.put("{", "{");
- textKeywords.put("}", "}");
- textKeywords.put(" ", " ");
- textKeywords.put("~", " ");
- textKeywords.put("_", "‚Äë");
- textKeywords.put("bullet", "•");
- textKeywords.put("emdash", "—");
- textKeywords.put("emspace", "‚ÄÉ");
- textKeywords.put("endash", "–");
- textKeywords.put("enspace", "‚ÄÇ");
- textKeywords.put("ldblquote", "“");
- textKeywords.put("lquote", "‘");
- textKeywords.put("ltrmark", "\u200e");
- textKeywords.put("rdblquote", "”");
- textKeywords.put("rquote", "’");
- textKeywords.put("rtlmark", "\u200f");
- textKeywords.put("tab", "\t");
- textKeywords.put("zwj", "\u200d");
- textKeywords.put("zwnj", "\u200c");
- textKeywords.put("-", "‚Äß");
- useNeXTForAnsi = false;
- characterSets = new Hashtable();
- }
-
- public RTFReader(StyledDocument var1) {
- this.target = var1;
- this.parserState = new Hashtable();
- this.fontTable = new Hashtable();
- this.rtfversion = -1;
- this.mockery = new MockAttributeSet();
- this.documentAttributes = new SimpleAttributeSet();
- }
-
- // $FF: synthetic method
- static Dictionary access$0() {
- return straightforwardAttributes;
- }
-
- // $FF: synthetic method
- static MockAttributeSet access$1(RTFReader var0) {
- return var0.mockery;
- }
-
- public void begingroup() {
- if (this.skippingCharacters > 0) {
- this.skippingCharacters = 0;
- }
-
- Object var1 = this.parserState.get("_savedState");
- if (var1 != null) {
- this.parserState.remove("_savedState");
- }
-
- Dictionary var2 = (Dictionary)((Hashtable)this.parserState).clone();
- if (var1 != null) {
- var2.put("_savedState", var1);
- }
-
- this.parserState.put("_savedState", var2);
- if (this.rtfDestination != null) {
- this.rtfDestination.begingroup();
- }
-
- }
-
- public void close() throws IOException {
- Enumeration var1 = this.documentAttributes.getAttributeNames();
-
- while(var1.hasMoreElements()) {
- Object var2 = var1.nextElement();
- this.target.putProperty(var2, this.documentAttributes.getAttribute((String)var2));
- }
-
- ((RTFParser)this).warning("RTF filter done.");
- super.close();
- }
-
- Color defaultColor() {
- return Color.black;
- }
-
- public static void defineCharacterSet(String var0, char[] var1) {
- if (var1.length < 256) {
- throw new IllegalArgumentException("Translation table must have 256 entries.");
- } else {
- characterSets.put(var0, var1);
- }
- }
-
- public void endgroup() {
- if (this.skippingCharacters > 0) {
- this.skippingCharacters = 0;
- }
-
- Dictionary var1 = (Dictionary)this.parserState.get("_savedState");
- Destination var2 = (Destination)var1.get("dst");
- if (var2 != this.rtfDestination) {
- this.rtfDestination.close();
- this.rtfDestination = var2;
- }
-
- Dictionary var3 = this.parserState;
- this.parserState = var1;
- if (this.rtfDestination != null) {
- this.rtfDestination.endgroup(var3);
- }
-
- }
-
- public static Object getCharacterSet(String var0) throws IOException {
- char[] var1 = (char[])characterSets.get(var0);
- if (var1 == null) {
- Object var2 = null;
-
- Class var3;
- try {
- var3 = Class.forName("javax.swing.text.rtf.RTFReader");
- } catch (ClassNotFoundException var5) {
- throw new InternalError("Help, I do not exist (" + var5 + ")");
- }
-
- InputStream var6 = var3.getResourceAsStream("charsets/" + var0 + ".txt");
- var1 = readCharset(var6);
- defineCharacterSet(var0, var1);
- }
-
- return var1;
- }
-
- public void handleBinaryBlob(byte[] var1) {
- if (this.skippingCharacters > 0) {
- --this.skippingCharacters;
- }
- }
-
- public boolean handleKeyword(String var1) {
- boolean var3 = this.ignoreGroupIfUnknownKeyword;
- if (this.skippingCharacters > 0) {
- --this.skippingCharacters;
- return true;
- } else {
- this.ignoreGroupIfUnknownKeyword = false;
- Object var2;
- if ((var2 = textKeywords.get(var1)) != null) {
- this.handleText((String)var2);
- return true;
- } else if (var1.equals("fonttbl")) {
- this.setRTFDestination(new FonttblDestination(this));
- return true;
- } else if (var1.equals("colortbl")) {
- this.setRTFDestination(new ColortblDestination(this));
- return true;
- } else if (var1.equals("stylesheet")) {
- this.setRTFDestination(new StylesheetDestination(this));
- return true;
- } else if (var1.equals("info")) {
- this.setRTFDestination(new InfoDestination(this));
- return false;
- } else if (var1.equals("mac")) {
- this.setCharacterSet("mac");
- return true;
- } else if (var1.equals("ansi")) {
- if (useNeXTForAnsi) {
- this.setCharacterSet("NeXT");
- } else {
- this.setCharacterSet("ansi");
- }
-
- return true;
- } else if (var1.equals("next")) {
- this.setCharacterSet("NeXT");
- return true;
- } else if (var1.equals("pc")) {
- this.setCharacterSet("cpg437");
- return true;
- } else if (var1.equals("pca")) {
- this.setCharacterSet("cpg850");
- return true;
- } else if (var1.equals("*")) {
- this.ignoreGroupIfUnknownKeyword = true;
- return true;
- } else if (this.rtfDestination != null && this.rtfDestination.handleKeyword(var1)) {
- return true;
- } else {
- if (var1.equals("aftncn") || var1.equals("aftnsep") || var1.equals("aftnsepc") || var1.equals("annotation") || var1.equals("atnauthor") || var1.equals("atnicn") || var1.equals("atnid") || var1.equals("atnref") || var1.equals("atntime") || var1.equals("atrfend") || var1.equals("atrfstart") || var1.equals("bkmkend") || var1.equals("bkmkstart") || var1.equals("datafield") || var1.equals("do") || var1.equals("dptxbxtext") || var1.equals("falt") || var1.equals("field") || var1.equals("file") || var1.equals("filetbl") || var1.equals("fname") || var1.equals("fontemb") || var1.equals("fontfile") || var1.equals("footer") || var1.equals("footerf") || var1.equals("footerl") || var1.equals("footerr") || var1.equals("footnote") || var1.equals("ftncn") || var1.equals("ftnsep") || var1.equals("ftnsepc") || var1.equals("header") || var1.equals("headerf") || var1.equals("headerl") || var1.equals("headerr") || var1.equals("keycode") || var1.equals("nextfile") || var1.equals("object") || var1.equals("pict") || var1.equals("pn") || var1.equals("pnseclvl") || var1.equals("pntxtb") || var1.equals("pntxta") || var1.equals("revtbl") || var1.equals("rxe") || var1.equals("tc") || var1.equals("template") || var1.equals("txe") || var1.equals("xe")) {
- var3 = true;
- }
-
- if (var3) {
- this.setRTFDestination(new DiscardingDestination(this));
- }
-
- return false;
- }
- }
- }
-
- public boolean handleKeyword(String var1, int var2) {
- boolean var3 = this.ignoreGroupIfUnknownKeyword;
- if (this.skippingCharacters > 0) {
- --this.skippingCharacters;
- return true;
- } else {
- this.ignoreGroupIfUnknownKeyword = false;
- if (var1.equals("uc")) {
- this.parserState.put("UnicodeSkip", new Integer(var2));
- return true;
- } else if (var1.equals("u")) {
- if (var2 < 0) {
- var2 += 65536;
- }
-
- ((RTFParser)this).handleText((char)var2);
- Number var4 = (Number)this.parserState.get("UnicodeSkip");
- if (var4 != null) {
- this.skippingCharacters = var4.intValue();
- } else {
- this.skippingCharacters = 1;
- }
-
- return true;
- } else if (var1.equals("rtf")) {
- this.rtfversion = var2;
- this.setRTFDestination(new DocumentDestination(this));
- return true;
- } else {
- if (var1.startsWith("NeXT") || var1.equals("private")) {
- var3 = true;
- }
-
- if (this.rtfDestination != null && this.rtfDestination.handleKeyword(var1, var2)) {
- return true;
- } else {
- if (var3) {
- this.setRTFDestination(new DiscardingDestination(this));
- }
-
- return false;
- }
- }
- }
- }
-
- public void handleText(String var1) {
- if (this.skippingCharacters > 0) {
- if (this.skippingCharacters >= var1.length()) {
- this.skippingCharacters -= var1.length();
- return;
- }
-
- var1 = var1.substring(this.skippingCharacters);
- this.skippingCharacters = 0;
- }
-
- if (this.rtfDestination != null) {
- this.rtfDestination.handleText(var1);
- } else {
- ((RTFParser)this).warning("Text with no destination. oops.");
- }
- }
-
- static char[] readCharset(InputStream var0) throws IOException {
- char[] var1 = new char[256];
- StreamTokenizer var3 = new StreamTokenizer(new InputStreamReader(var0));
- var3.eolIsSignificant(false);
- var3.commentChar(35);
- var3.slashSlashComments(true);
- var3.slashStarComments(true);
-
- for(int var2 = 0; var2 < 256; ++var2) {
- int var4;
- try {
- var4 = var3.nextToken();
- } catch (Exception var6) {
- throw new IOException("Unable to read from character set file (" + var6 + ")");
- }
-
- if (var4 != -2) {
- throw new IOException("Unexpected token in character set file");
- }
-
- var1[var2] = (char)((int)var3.nval);
- }
-
- return var1;
- }
-
- static char[] readCharset(URL var0) throws IOException {
- return readCharset(var0.openStream());
- }
-
- public void setCharacterSet(String var1) {
- Object var2;
- try {
- var2 = getCharacterSet(var1);
- } catch (Exception var5) {
- ((RTFParser)this).warning("Exception loading RTF character set \"" + var1 + "\": " + var5);
- var2 = null;
- }
-
- if (var2 != null) {
- super.translationTable = (char[])var2;
- } else {
- ((RTFParser)this).warning("Unknown RTF character set \"" + var1 + "\"");
- if (!var1.equals("ansi")) {
- try {
- super.translationTable = (char[])getCharacterSet("ansi");
- } catch (IOException var4) {
- throw new InternalError("RTFReader: Unable to find character set resources (" + var4 + ")");
- }
- }
- }
-
- this.setTargetAttribute("rtfCharacterSet", var1);
- }
-
- protected void setRTFDestination(Destination var1) {
- Dictionary var2 = (Dictionary)this.parserState.get("_savedState");
- if (var2 != null && this.rtfDestination != var2.get("dst")) {
- ((RTFParser)this).warning("Warning, RTF destination overridden, invalid RTF.");
- this.rtfDestination.close();
- }
-
- this.rtfDestination = var1;
- this.parserState.put("dst", this.rtfDestination);
- }
-
- private void setTargetAttribute(String var1, Object var2) {
- }
- }
-